package com.lzh.runke.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.lzh.runke.entity.TWxfwZyld;
import com.lzh.runke.entity.spec.SolidWasteSpec;
import com.lzh.runke.mapper.SolidWasteMapper;
import com.lzh.runke.service.ISolidWasteService;
import com.lzh.runke.util.fromyibei.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 开发中常见导出场景
 * 1.文件导出
 * 2.异步导出：上传到文件服务器，然后异步下载
 * 3.csv导出
 * 4.压缩导出
 * 5.动态导出
 * 6.easyExcel导出
 *
 * @author LiuZiHao
 * @date 创建时间:2023年03月22日 10:53:20
 */
@Slf4j
@RestController
@RequestMapping("/export")
public class ExportController {

//    @Resource
//    private xxxxService service;

//    /**
//     * 1.文件导出
//     * eg:多断面-月度变化-列表导出(wms)
//     *
//     * @param spec MultiSectionSpec
//     * @return ModelAndView
//     */
//    @RequestMapping("/exportMultiSectionMonthlyList")
//    public ModelAndView exportMultiSectionMonthlyList(@RequestBody @Validated Object spec) {
//        Result<List<Object>> result = new Result<>();
////        Result<List<Object>> result = service.getXXXX(spec);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("spec", spec);
//        modelAndView.addObject("data", result.getData());
//        modelAndView.addObject("extra", result.getExtra());
//        modelAndView.setViewName("CommonView");
//        return modelAndView;
//    }


    @Resource
    private ISolidWasteService solidWasteService;
    @Resource
    private SolidWasteMapper solidWasteMapper;


    /**
     * 1.poi导出
     * 固体废物-转移联单-导出
     *
     * @param spec
     * @return
     */
    @RequestMapping("/exportTransferCopyPoi")
    public ModelAndView ExportTransferCopyList(@RequestBody SolidWasteSpec spec) {
        StopWatch stopWatch = DateUtil.createStopWatch("poi导出");
        stopWatch.start("从数据库插数据");
        ModelAndView modelAndView = new ModelAndView();
        log.info("固体废物-转移联单-导出...");
        log.debug("参数校验...");
        log.debug("查数据...");
        PageHelper.startPage(spec.getPageNo(), spec.getPageSize(), false);
        List<TWxfwZyld> dataList = solidWasteMapper.loadTransferCopyBySpec(spec.getDisposalMethods(),
                spec.getWasteForms(),
                spec.getJointOrderStatus(),
                spec.getTransfertStartTime(),
                spec.getTransferEndTime(),
                spec.getAcceptStartTime(),
                spec.getAcceptEndTime(),
                spec.getKeyword());
        HashMap<String, Object> mapData = Maps.newHashMap();
        mapData.put("spec", spec);
        mapData.put("data", dataList);
        modelAndView.setViewName("TransferCopyView");
        modelAndView.addObject("result", mapData);
        stopWatch.stop();
        return modelAndView;
    }

    /**
     * 3.CSV导出
     *
     * @param spec
     * @return
     */
    @RequestMapping(value = "/exportTransferCopyList", produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE})
    public ResponseEntity<byte[]> exportTransferCopyList(@RequestBody SolidWasteSpec spec) {
        log.info("固体废物-转移联单-导出...");
        return solidWasteService.exportTransferCopyList(spec);
    }

    /**
     * 6.easyExcel导出
     *
     * @return
     */
    @GetMapping("/exportEasyExcel")
    public void exportEasyExcel(HttpServletResponse response) {
        SolidWasteSpec spec = new SolidWasteSpec();
        StopWatch stopWatch = DateUtil.createStopWatch("EasyExcel导出");
        stopWatch.start("从数据库插数据");
        List<TWxfwZyld> dataList = solidWasteMapper.loadTransferCopyBySpec(spec.getDisposalMethods(),
                spec.getWasteForms(),
                spec.getJointOrderStatus(),
                spec.getTransfertStartTime(),
                spec.getTransferEndTime(),
                spec.getAcceptStartTime(),
                spec.getAcceptEndTime(),
                spec.getKeyword());
        stopWatch.stop();

        stopWatch.start("导出");
        ExcelUtil.exportExcel(dataList, "转移联单导出", TWxfwZyld.class, response);
        stopWatch.stop();

    }


}
